package com.zh.zcy.redis;


import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;

/**
 * Created by TOPFEEL on 2019/5/28.
 */
@Configuration
@EnableCaching
public class RedisCacheConfig {

    @Bean(name = "cacheManager")
    @Primary
    public CacheManager cacheManager(RedisConnectionFactory factory, RedisTemplate customRedisTemplate){
        //可以配置缓存过期时间，是否缓存null值，配置前缀，配置数据转换器
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(customRedisTemplate.getValueSerializer()));

        RedisCacheManager cacheManager = RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter(factory))
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }

    @Bean(name = "cacheManager_1s")
    public CacheManager cacheManager_1s(RedisConnectionFactory factory, RedisTemplate customRedisTemplate){
        //可以配置缓存过期时间，是否缓存null值，配置前缀，配置数据转换器
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(1))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(customRedisTemplate.getValueSerializer()));

        RedisCacheManager cacheManager = RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter(factory))
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }


    @Bean(name = "cacheManager_1m")
    public CacheManager cacheManager_1m(RedisConnectionFactory factory, RedisTemplate customRedisTemplate){
        //可以配置缓存过期时间，是否缓存null值，配置前缀，配置数据转换器
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(60))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(customRedisTemplate.getValueSerializer()));

        RedisCacheManager cacheManager = RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter(factory))
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }

    @Bean(name = "cacheManager_5m")
    public CacheManager cacheManager_5m(RedisConnectionFactory factory, RedisTemplate customRedisTemplate){
        //可以配置缓存过期时间，是否缓存null值，配置前缀，配置数据转换器
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(60 * 5))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(customRedisTemplate.getValueSerializer()));

        RedisCacheManager cacheManager = RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter(factory))
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }

    @Bean(name = "cacheManager_10m")
    public CacheManager cacheManager_10m(RedisConnectionFactory factory, RedisTemplate customRedisTemplate){
        //可以配置缓存过期时间，是否缓存null值，配置前缀，配置数据转换器
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(60 * 10))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(customRedisTemplate.getValueSerializer()));

        RedisCacheManager cacheManager = RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter(factory))
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }

}
